package cn.fsd.controller;

import cn.fsd.domain.CommodityClass;
import cn.fsd.query.delivery.ClassQuery;
import cn.fsd.service.impl.CommodityClassServiceImpl;
import cn.fsd.util.Constants;
import cn.fsd.vo.ClassItem;
import cn.fsd.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/class")

public class CommodityClassController {

  @Autowired
  CommodityClassServiceImpl commodityClassService;

  @GetMapping("/all")
  @ApiOperation(value = "获取全部商品分类，以树状结果返回")
  public ResultBean getAllCommodityClass() {
    return ResultBean.success(commodityClassService.getAllClasses());
  }

  @GetMapping("/parent")
  @ApiOperation(value = "获取全部一级分类")
  public ResultBean getAllCommodityFirstClass() {
    return ResultBean.success(commodityClassService.getFirstClasses());
  }

  @GetMapping("/{id}")
  @ApiOperation(value = "获取到id对应的分类")
  public ResultBean getClass(@PathVariable("id") String id) {
    return ResultBean.success(commodityClassService.getById(id));
  }

  @PostMapping("/page")
  @ApiOperation(value = "分页查询分类")
  public ResultBean getCommodityClassByPage(@RequestBody ClassQuery query) {
    if (Objects.isNull(query)) {
      return ResultBean.error("请求参数不能为空");
    }
    return ResultBean.success(commodityClassService.getClassesByPage(query));
  }

  @PostMapping
  @ApiOperation(value = "新建分类")
  public ResultBean addClass(@RequestBody ClassItem newClass) {
    if (StringUtils.isEmpty(newClass.getName())) {
      return ResultBean.error("分类名称不能为空");
    } else if (!Constants.PARENT.equals(newClass.getLevel()) && !Constants.CHILD.equals(
        newClass.getLevel())) {
      return ResultBean.error("分类级别错误");
    } else if (Constants.CHILD.equals(newClass.getLevel()) && StringUtils.isEmpty(
        newClass.getParentId())) {
      return ResultBean.error("父级分类的id不能为空");
    } else if (isDuplicate(newClass.getName())) {
      return ResultBean.error("分类名称重复");
    }
    return ResultBean.success((Object)commodityClassService.addClass(newClass));
  }

  @DeleteMapping("/{id}")
  @ApiOperation(value = "根据id删除分类，如果对应为子级分类直接删除，如果对应父级分类将所有子级分类删除")
  public ResultBean deleteClassById(@PathVariable("id") String id) {
    commodityClassService.deleteById(id);
    return ResultBean.success();
  }

  @PutMapping
  @ApiOperation(value = "更新分类信息")
  public ResultBean updateClassInfo(@RequestBody ClassItem item) {
    if (StringUtils.isEmpty(item.getClassId())) {
      return ResultBean.error("分类id不能为空");
    } else if (StringUtils.isEmpty(item.getName())) {
      return ResultBean.error("分类名称不能为空");
    } else if (!Constants.PARENT.equals(item.getLevel()) && !Constants.CHILD.equals(
        item.getLevel())) {
      return ResultBean.error("分类级别错误");
    } else if (Constants.CHILD.equals(item.getLevel()) && StringUtils.isEmpty(item.getParentId())) {
      return ResultBean.error("父级分类的id不能为空");
    }
    CommodityClass e = commodityClassService.getByName(item.getName());
    if (e != null && !Objects.equals(e.getClass_id(), item.getClassId())) {
      return ResultBean.error("分类名称重复");
    }
    commodityClassService.updateClass(item);
    return ResultBean.success();
  }


  /**
   * 通过分类名称查询是否有相同名称的分类
   *
   * @param name 分类的名称
   * @return true 有相同名称分类 false 没有相同名称分类
   */
  public boolean isDuplicate(String name) {
    CommodityClass item = commodityClassService.getByName(name);
    return item != null;
  }

  /**
   * 微服务需要调用的接口
   */
  @GetMapping("/getClassById")
  public CommodityClass getClassById(@RequestParam("classId")String classId){
    return commodityClassService.getClassById(classId);
  }


}
